package com.pks.datacenter.config;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
	
	private OpenApiExtensionResolver openApiExtensionResolver; 
	 
	@Autowired
    public void SwaggerConfiguration(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }
	
	@Bean(value = "defaultApi2")
	public Docket defaultApi2(){
		return new Docket(DocumentationType.SWAGGER_2)
		.useDefaultResponseMessages(false)
		.apiInfo(apiInfo())
		.select()
		//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
        .apis(RequestHandlerSelectors.basePackage("com.pks.datacenter.controller"))
        .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        //指定路径处理PathSelectors.any()代表所有的路径
        .paths(PathSelectors.any())
        .build()
        .extensions(openApiExtensionResolver.buildExtensions("1.0"));
	}
	
	
	private ApiInfo apiInfo(){
		return new ApiInfoBuilder()
		.title("接口文档")
		.termsOfServiceUrl("http://127.0.0.1/")
		.contact(new Contact("数据中心", null, null))
		.version("1.0")
		.build();
	}
}
